﻿Public Class setCursoAlumno
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lblMsg.Text = ""

        'Carga de datos del alumno
        Dim sDNI As String = Request("dni")
        Dim dni As Integer

        sDNI = "25791115"

        If Not Integer.TryParse(sDNI, dni) Then
            Panel1.Visible = False
            Panel2.Visible = False
            lblMsg.Text = "Datos relacinados incorrectos"
            Return
        End If

        Dim tAlumnos As New LocalDataTableAdapters.AlumnosTableAdapter
        Dim rAlumnos As New LocalData.AlumnosDataTable

        tAlumnos.Fill(rAlumnos)

        Dim rr() As LocalData.AlumnosRow = rAlumnos.Select("dni = " & dni.ToString)

        If rr.Length <> 1 Then
            Panel1.Visible = False
            Panel2.Visible = False
            lblMsg.Text = "Datos relacinados incorrectos (dni)."
            Return
        End If

        Dim r As LocalData.AlumnosRow = rr(0)
        lblApellido.Text = r.Apellidos
        lblNombre.Text = r.Nombres
        lblCurActual.Text = fnCursoActual(dni)

        ddlAnio.Items.Clear()

        Dim tCursos As New LocalDataTableAdapters.CursosTableAdapter
        Dim rCursos As New LocalData.CursosDataTable

        tCursos.Fill(rCursos)

        tCursos.Connection.Open()
        Dim comm As New Data.SqlClient.SqlCommand("SELECT DISTINCT Anio FROM Cursos", tCursos.Connection)

        Dim dr As Data.SqlClient.SqlDataReader = comm.ExecuteReader

        While dr.Read
            ddlAnio.Items.Add(dr(0).ToString)
        End While

        dr.Close()

        comm.CommandText = "SELECT DISTINCT Comision FROM Cursos"
        dr = comm.ExecuteReader

        ddlComision.Items.Clear()
        While dr.Read
            ddlComision.Items.Add(dr(0).ToString)
        End While


    End Sub

    Private Function fnCursoActual(dni As Integer) As String
        Dim rCursoAlumno As New LocalData.CursoAlumnoDataTable
        Dim tCursoAlumno As New LocalDataTableAdapters.CursoAlumnoTableAdapter

        tCursoAlumno.Fill(rCursoAlumno)

        Dim ca() As LocalData.CursoAlumnoRow = rCursoAlumno.Select("(dni = " & dni.ToString & ") AND (Valido = 'Si')")

        If ca.Length > 0 Then
            If ca.Length > 1 Then
                Return "Error: Mas de un curso"
            End If

            Dim tCurso As New LocalDataTableAdapters.CursosTableAdapter
            Dim rCurso As New LocalData.CursosDataTable

            tCurso.Fill(rCurso)

            Dim c() As LocalData.CursosRow = rCurso.Select("id = " & ca(0).idCurso)

            If c.Length = 1 Then
                Dim sFormat As String = "{0}º {1}ª Turno {2}, {3} ({4})"
                sFormat = String.Format(sFormat, c(0).Anio, c(0).Comision, c(0).Turno, c(0).Modalidad, c(0).Cursada)
                Return sFormat
            End If
        End If
        Return "Sin curso asignado"
    End Function

    Protected Sub btnSet_Click(sender As Object, e As EventArgs) Handles btnSet.Click
        'Valido datos ingresados
        Dim retValidar As String = ""
        If Not Validar(retValidar) Then
            lblMsg.Text = "Datos incorrectos: " & retValidar
        End If

        'Guardo el curso anterior

    End Sub

    Private Function Validar(ByRef retValidar As String) As Boolean


        Return True
    End Function
End Class